package app_kvEcs;

import common.messages.KVMessage;

import utils.CommonUtil;

/**
 * @author Cipri_L
 * Maintains the data for a ServerNode.
 */
public class ServerNode {

	private boolean running;
	private String name;
	private String ip;
	private String port;
	private String position;
	
	private String startRange;
	private String endRange;	
	//used for the case when we have from 00 to the left of FF
	private boolean extendedRange;
	private String startRangeExtended;
	private String endRangeExtended;
	
	private ServerNode next  ;
	private ServerNode previous ;
	
	private KVStoreECS server;
	private boolean allocated;
	private boolean modified;
	
	public ServerNode(String name, String ip, String port){
		this.name = name;
		this.ip = ip;
		this.port = port;	
		position = CommonUtil.hashString(ip+":"+port);
		this.running = false;
		next = null;
		previous = null;
		server = new KVStoreECS(ip, Integer.valueOf(port));
		allocated = false;
		extendedRange = false;
		modified = false;
	}
	
	public boolean isAllocated(){
		return allocated;
	}
	
	public void setAllocated(boolean value){
		this.allocated = value;
	}
	
	public boolean connected(){
		return server.isConnectionEstablished();
	}
	
	public KVStoreECS server(){
		return server;
	}
	
	public KVMessage updateRange() throws Exception{
		KVMessage response = server.updateRange(startRange, endRange);
		modified = false;
		return response;
	}
	
	public boolean isModified(){
		return modified;
	}
	
	public String startRange() {
		return startRange;
	}

	public void setStartRange(String startRange) {
		modified = true;
		this.startRange = startRange;
	}

	public String endRange() {
		return endRange;
	}

	public void setEndRange(String endRange) {
		modified = true;
		this.endRange = endRange;
	}
	
	public boolean extendedRange(){
		return extendedRange;
	}
	
	public void setExtendedRange(boolean extended){
		modified = true;
		extendedRange = extended;
		if(extendedRange==false){
			endRangeExtended = "";
			startRangeExtended = "";
		}
	}
	
	public String startRangeExtended() {
		return startRangeExtended;
	}

	public void setStartRangeExtended(String startRange) {
		modified = true;
		this.startRangeExtended = startRange;
	}

	public String endRangeExtended() {
		return endRangeExtended;
	}

	public void setEndRangeExtended(String endRange) {
		modified = true;
		this.endRangeExtended = endRange;
	}
	
	public ServerNode next() {
		return next;
	}

	public ServerNode previous(){
		return previous;
	}
	
	public void setPrevious(ServerNode previous){
		this.previous = previous;
	}
	
	public void setNext(ServerNode next) {
		this.next = next;
	}

	public void setRunningOn(){
		this.running = true;
	}
	
	public void setRunningOff(){
		this.running = false;
	}
	
	public boolean isRunning(){
		return this.running;
	}
	
	public String name(){
		return this.name;
	}
	
	public String ip(){
		return ip;
	}
	
	public String port(){
		return port;
	}
	
	public String getPosition(){
		return position;
	}
	
	
	
	public String toString(){
		return name+":"+ip+":"+port+"%"+position;
	}
	
	public boolean equals(Object o){
		if ( o == null)
			return false;
		if( !(o instanceof ServerNode))
			return false;
		return this.toString().equals(o.toString());
	}
	
	public String info(){
		String result = "";
		String extended = "";
		if(this.extendedRange){
			extended = "{" + this.startRangeExtended +"-"+ this.endRangeExtended +"}";  
		}
		result = this.name +" ";
		result += "[" + startRange +"-"+ position + "-" + endRange + "]";
		result += extended;
		return result;
	}
}
